//
//  ViewController.m
//  UIHomeWork19_汽车之家找车页
//
//  Created by tusm on 16/8/9.
//  Copyright © 2016年 tusm. All rights reserved.
//

#import "ViewController.h"
#import "AllBrandlist.h"
#import "HotBrandList.h"
#import "ExtensionList.h"
#import "HotBrandTableViewCell.h"
#import "ExtensionTableViewCell.h"
#import "AllTableViewCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *AlltableView;
@property (nonatomic,strong)NSMutableArray <AllBrandlist *> *allBrandListArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.edgesForExtendedLayout = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self handleURL];
    [self createTableView];
    
    
}


- (void)handleURL{
    [self handleAllUrl];
  
}









- (void)handleAllUrl{

    NSURL *allUrl = [NSURL URLWithString:@"http://app.api.autohome.com.cn/autov4.6/cars/brands-a2-pm2-v4.6.1-ts635580073583652962.html"];
    NSMutableURLRequest *allURLRequest = [NSMutableURLRequest requestWithURL:allUrl];
    allURLRequest.HTTPMethod = @"GET";
    NSURLSession *urlsession = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [urlsession dataTaskWithRequest:allURLRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSError *error = nil;
            NSDictionary *Alldic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
            NSArray *brandlistArray = Alldic[@"result"][@"brandlist"];
            self.allBrandListArr = [NSMutableArray array];
            for (NSDictionary *dic in brandlistArray) {
                AllBrandlist *allBrandList = [[AllBrandlist alloc] initWithDictionary:dic];
                [_allBrandListArr addObject:allBrandList];
            }
            [_AlltableView reloadData];
        });
    }];

    [dataTask resume];
}






- (void)createTableView{

    [_AlltableView registerNib:[UINib nibWithNibName:@"AllTableViewCell" bundle:nil] forCellReuseIdentifier:@"poolAll"];
    
    [_AlltableView registerClass:[HotBrandTableViewCell class] forCellReuseIdentifier:@"poolHot"];

    [_AlltableView registerClass:[ExtensionTableViewCell class] forCellReuseIdentifier:@"poolExtension"];
}

//返回分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSLog(@"!!!!!!!!!!!!!!!!!!!!!!!!%ld",_allBrandListArr.count);
        return  _allBrandListArr.count + 2;
}
//返回cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 200;
    }else if(indexPath.section == 1){
        return 150;
    }else{
        return 50;
    }
}
//返回分区的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        NSString *title = @"热门品牌";
        return title;
    }else if (section == 1){
    NSString *title = @"主打车";
        return title;
    }
    else{
        return _allBrandListArr[section - 2].letter;
    }

}
//返回每个section中的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0 || section == 1) {
        return 1;
    }
    else{
        AllBrandlist *allbrandList = _allBrandListArr[section - 2];
        return allbrandList.list.count;
    }

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        HotBrandTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolHot"];
        cell.hotBrandArray = cell.hotBrandListArr;
        return cell;
    }else if (indexPath.section == 1){
        ExtensionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolExtension"];
        cell.extensionArray = cell.extensionListArr;
        return cell;
    }
    
    else{
        AllTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolAll"];
        AllBrandlist *allBrandList = _allBrandListArr[indexPath.section - 2];
        AllList *allList = allBrandList.list[indexPath.row];
        cell.allList = allList;
        return cell;
    }
}

//索引
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {

    NSMutableArray *indexesArr = [NSMutableArray array];
    [indexesArr addObject:@"热"];
    [indexesArr addObject:@"主"];
    for (AllBrandlist *allBrandList in _allBrandListArr) {
        [indexesArr addObject:allBrandList.letter];
    }
    return indexesArr;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
